﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business_Layer;

using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Label2.Visible = false;
            Search_Product_BUS dac = new Search_Product_BUS();
            DataTable dt = null;
            int speed = int.Parse(Request["Speed"].ToString());
            if (speed == 1)
            {
                string giatu = Request["giatu"].ToString();
                string giaden = Request["giaden"].ToString();
                dt = dac.SearchPrice(float.Parse(giatu), float.Parse(giaden));
            }
            else
            {
                string[] value = new string[5];
                int[] k = new int[5] { 0, 0, 0, 1, 0 };
                value[0] = Request["giatu"].ToString();
                value[1] = Request["giaden"].ToString();
                value[2] = Request["tensp"].ToString();
                value[3] = Request["loaisp"].ToString();
                if (value[3].CompareTo("-1") == 0)
                {
                    k[3] = 0;
                }
                value[4] = Request["tentg"].ToString();

                for (int i = 0; i < value.Length; i++)
                {
                    if (value[i].CompareTo("0") != 0 && i != 3)
                    {
                        k[i] = 1;
                    }
                }

                //0:giatu 1:giaden 2:tensp 4:tentg 3:loaisp
                if (k[0] == 1 && k[1] == 1 && k[2] == 1 && k[3] == 1 && k[4] == 1)
                {
                    dt = dac.SearchALL(float.Parse(value[0]), float.Parse(value[1]), value[2], value[4], int.Parse(value[3]));
                }
                if (k[0] == 0 && k[1] == 0 && k[2] == 1 && k[3] == 0 && k[4] == 0)
                {
                    dt = dac.SearchTenSP(value[2]);
                }
                if (k[0] == 0 && k[1] == 0 && k[2] == 0 && k[3] == 1 && k[4] == 0)
                {
                    dt = dac.SearchLoaiSP(int.Parse(value[3]));
                }
                if (k[0] == 0 && k[1] == 0 && k[2] == 0 && k[3] == 0 && k[4] == 1)
                {
                    dt = dac.SearchTacGia(value[4]);
                }
                if (k[0] == 1 && k[1] == 1 && k[2] == 0 && k[3] == 0 && k[4] == 0)
                {
                    dt = dac.SearchPrice(float.Parse(value[0]), float.Parse(value[1]));
                }
                if (k[0] == 1 && k[1] == 0 && k[2] == 0 && k[3] == 0 && k[4] == 0)
                {
                    dt = dac.SearchPrice(float.Parse(value[0]), 900000000);
                }
                if (k[0] == 0 && k[1] == 1 && k[2] == 0 && k[3] == 0 && k[4] == 0)
                {
                    dt = dac.SearchPrice(0, float.Parse(value[1]));
                }
            }
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = 6; // số items hiển thị trên một trang.
            CollectionPager1.DataSource = dt.DefaultView;
            CollectionPager1.BindToControl = DataList1;
            DataList1.DataSource = CollectionPager1.DataSourcePaged;
            DataList1.DataBind();
        }
        catch 
        {
            Label2.Text = "Không tìm thấy như yêu cầu";
            Label2.Visible = true;
        }
    }
    protected void btnTim_Click(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void btnImage_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton btn = (ImageButton)sender;
        int ProductID = int.Parse(btn.CommandArgument);

        Response.Redirect("ProductDetail.aspx?ProductID=" + ProductID);
    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}